package com.dengqi.mymodule.tools;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

import java.io.File;

/**
 * https://blog.csdn.net/u011840744/article/details/72458052
 */
public class BitmapUtils {

    /**
     * file to bitmap
     * @param file
     * @return
     */
    public static Bitmap file2Bitmap(File file, int widthPixels, int heightPixels) {
        return createImageThumbnail(file.getPath(), widthPixels, heightPixels);
    }

    /**
     * 弃用
     * 7.0以上uri转不了file
     * uri to bitmap
     */
    public static Bitmap uri2Bitmap(Uri uri, int widthPixels, int heightPixels) {
            //uri太大会内存溢出
//            return MediaStore.Images.Media.getBitmap(context.getApplicationContext().getContentResolver(), uri);
        return createImageThumbnail(uri.getPath(), widthPixels, heightPixels);
    }


    /**
     * 获取缩略图
     * @param filePath
     * @return
     */
    public static Bitmap createImageThumbnail(String filePath, int widthPixels, int heightPixels){
        Bitmap bitmap = null;
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, opts);
        opts.inSampleSize = computeSampleSize(opts, -1, widthPixels*heightPixels);
        //为了读到图片,必须把options.inJustDecodeBounds设回false
        opts.inJustDecodeBounds = false;

        try {
            bitmap = BitmapFactory.decodeFile(filePath, opts);
        }catch (Exception e) {
            // TODO: handle exception
        }
        return bitmap;
    }

    /**
     *
     * @param原本Bitmap的options
     * @param生成的缩略图的宽高中的较小的值
     * @param生成的缩量图的总像素
     * @return
     */
    public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }
        return roundedSize;
    }

    /**
     *
     * @param原本Bitmap的options
     * @param生成的缩略图的宽高中的较小的值
     * @param生成的缩量图的总像素
     * @return
     */
    private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;
        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 :(int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }
        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }

}
